﻿using System;
using System.Linq;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.DanhMuc
{
    public partial class FormNhanVien : DevComponents.DotNetBar.RibbonForm
    {
        //private AppAccContext db = new AppAccContext();
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        public FormNhanVien()
        {
            InitializeComponent();
        }
        private int idtam = 0;
        private void BttThemMoi_Click(object sender, EventArgs e)
        {
            var f = new FormNhanVienThem();
            f.ShowDialog();
        }

        private void BttChỉnhSua_Click(object sender, EventArgs e)
        {
            var f = new FormNhanVienCapNhap {Id = idtam};
            f.ShowDialog();
        }

        private void BttXoa_Click(object sender, EventArgs e)
        {
            if (idtam!=0)
            {
                Delete();
            }
            else
            {
                MessageBox.Show("Chưa chọn nhân viên để xóa!");
            }
        }

        private void BttLamMoi_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void FormNhanVien_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        // click
        private void dgvNhanVien_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var rowindex = e.RowIndex;
            var row = dgvNhanVien.Rows[rowindex];
            idtam = Convert.ToInt32(row.Cells[0].Value);
            KiemTra();
        }
        // Double click
        private void dgvNhanVien_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var rowindex = e.RowIndex;
            var row = dgvNhanVien.Rows[rowindex];
            idtam = Convert.ToInt32(row.Cells[0].Value);
            KiemTra();
            var f = new FormNhanVienCapNhap {Id = idtam};
            f.ShowDialog();
        }

        private void LoadData()
        {
            dgvNhanVien.DataSource = db.NhanViens.ToList();
            dgvNhanVien.Refresh();
            Reset();
        }

        private void Delete()
        {
            try
            {
                var t = db.NhanViens.Find(idtam);
                if (
                    MessageBox.Show("Bạn muốn xóa nhân viên: " + t.TenNhanVien + "!", "Xác nhận", MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    db.NhanViens.Remove(t);
                    db.SaveChanges();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
        void Reset()
        {
            idtam = 0;
            bttChinhSua.Enabled = false;
            bttXoa.Enabled = false;
        }
        void KiemTra()
        {
            if (idtam != 0)
            {
                bttChinhSua.Enabled = true;
                bttXoa.Enabled = true;
            }
            else
            {
                bttChinhSua.Enabled = false;
                bttXoa.Enabled = false;
            }
        }
        private void bttThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
